<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="首页模块" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用于首页区域板块展示">
          <a-select
            placeholder="请选择首页模块"
            v-decorator="['type_id', { rules: [{ required: true , message: '请选择首页模块'}] }]"
            @change="handleChange"
          >
            <a-select-option
              v-for="(item, index) in TypeList"
              :key="index"
              :value="item.type_id"
            >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="分类" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用于图片上方分类标题展示">
          <a-select
            placeholder="请选择分类"
            v-decorator="['second_type_id', { rules: [{ required: true , message: '请选择分类'}] }]"
          >
            <a-select-option
              v-for="(item, index) in TypeList2"
              :key="index"
              :value="item.type_id"
            >{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          label="图片"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra="建议尺寸: 300*300"
        >
          <SelectImage
            :defaultList="record.logoImage ? [record.logoImage] : []"
            v-decorator="['image_id', {rules: [{required: true, message: '请上传图片'}]}]"
          />
        </a-form-item>
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用于图片下方标题显示">
          <a-input v-decorator="['imagename']" />
        </a-form-item>
        <a-form-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用于图片下方文字描述">
          <a-input v-decorator="['describe']" />
        </a-form-item>
        <a-form-item label="商品ID" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用于跳转图片对应的商品">
          <a-input v-decorator="['goods_id']" />
        </a-form-item>
        <a-form-item label="商品分类ID" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用于跳转图片对应的商品分类">
          <a-input v-decorator="['category_id']" />
        </a-form-item>
        <a-form-item label="跳转地址" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="跳转自定义链接地址">
          <a-input v-decorator="['link',]" />
        </a-form-item>
        <a-form-item label="锁定状态" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="用户端是否锁定">
          <a-radio-group v-decorator="['lock_status', {initialValue: 1, rules: [{required: true}]}]">
            <a-radio :value="1">锁定</a-radio>
            <a-radio :value="0">解锁</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="自定义排序,数字越小越靠前">
          <a-input-number
            :min="0"
            v-decorator="['sort', {initialValue: 100, rules: [{required: true, message: '请输入排序值'}]}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/store/indexbanner'
import { SelectRegion, SelectImage } from '@/components'
export default {
  components: {
    SelectRegion,
    SelectImage
  },
   props: {
    // 会员等级列表
    TypeList: {
      type: Array,
      required: true
    },
  },
  data () {
    return {
      // 对话框标题
      title: '新增首页内容',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
       // 当前记录详情
       record: {},
       TypeList2: []
    }
  },
  methods: {

    /**
     * 显示对话框
     */
    add () {
      // 显示窗口
      this.visible = true
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    handleChange (value) {
      Api.selectType2({ type_id: value }).then(result => {
        this.TypeList2 = result.data.list
        this.record.name = ''
        // 设置默认值
        this.setFieldsValue()
      })
      console.log(`selected ${value}`, value)
    },

     /**
     * 设置默认值
     */
     setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, []))
      })
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      Api.add({ form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.confirmLoading = false
        })
    }

  }
}
</script>
